/**
 * Created by yangfan on 2016/10/18.
 */
define(['config/appregister',"i18n","business/personMine/services/httpServices"],function(app,i18n,httpService){
    app.controller('othersQuestionsCtrl', function($scope,$state,$stateParams) {
        $scope.i18n = i18n;

        // 切换title
        $scope.isDataChecked = true;
        $scope.isThesisChecked = false;
        $scope.checkTitle = function(type){
            $scope.listParams.keyWord = "";
            $scope.isCurrent = [];
            $scope.isCurrent[0] = "bold";
            $scope.listParams.pageNo = 1;
            $scope.listParams.pageSize = 10 ;
            if(type == "data"){
                $scope.isDataChecked = true;
                $scope.isThesisChecked = false;
                init();
            }else if(type == "thesis"){
                $scope.isThesisChecked = true;
                $scope.isDataChecked = false;
                getMyAnswerList();
            }
        };
        if( !angular.isUndefined($stateParams.userId)){
            $scope.userId = $stateParams.userId;
            $scope.$emit('getOthersId',{userId:$scope.userId})
        }
        $scope.tableTitle = ["问题","悬赏(积分)","回答","状态","提问时间"];
        $scope.titleNavation = [
            {title:"Ta提问的",type:"myQuestions"},
            {title:"Ta回答的",type:"myAnswer"}
        ];
        $scope.isCurrent = [];
        $scope.isCurrent[0] = "bold";
        $scope.listParams = {
            pageNo:1,
            pageSize:10
        };

        function getPageModel (){
            $scope.paginationConf = {
                currentPage: $scope.listParams.pageNo,
                totalItems: $scope.totalItem,
                //每页预览条数
                itemsPerPage: $scope.listParams.pageSize,
                //分页总数
                numberOfPages:Math.ceil($scope.totalItem/$scope.listParams.pageSize),
                // 实际分页长度
                pagesLength: 5,
                perPageOptions: [10, 20, 30, 40, 50],
                onChange: function () {
                    $scope.listParams.pageNo = $scope.paginationConf.currentPage;
                    getList()
                }

            };
            $scope.everPageShow = $scope.paginationConf.perPageOptions;
        }

        function init(){
            var req = {
                type:"GET",
                data:{
                    pageNo:$scope.listParams.pageNo,
                    pageSize:$scope.listParams.pageSize,
                    userId:$scope.userId
                },
                uri:"getPostbarByUserId"
            };
            httpService.getMyQuestionList(req).success(function(response){
                $scope.$apply(function(){
                    $scope.myDownload = response.data.list;
                    $scope.myDownload.ecoin = response.data.list.downloadCount*response.data.list.price;
                    $scope.totalItem = response.data.Pagination.totalCount;
                    getPageModel ()
                })
            }).error(function(){
                console.log("访问出错");
            })
        }

        function getMyAnswerList(){
            var req = {
                type:"GET",
                data:{
                    pageNo:$scope.listParams.pageNo,
                    pageSize:$scope.listParams.pageSize,
                    userId :$scope.userId
                },
                uri:"getReplyByUserId"
            };
            httpService.getMyAnswerList(req).success(function(response){
                $scope.$apply(function(){
                    $scope.myDownload = response.data.list;
                    //todo 待分类 国内国际
                    $scope.myDownload.ecoin = response.data.list.downloadCount*response.data.list.price;
                    $scope.totalItem = response.data.Pagination.totalCount;
                })
            }).error(function(){
                console.log("访问出错");
            })
        }

        function getList(){
            if($scope.isDataChecked == true){
                init()
            }else{
                getMyAnswerList()
            }
        }
        getList()

        //改变每页预览条数
        $scope.changeItemsPerPage = function(item ,index){
            $scope.isCurrent = [];
            $scope.isCurrent[index] = "bold";

            $scope.listParams.pageNo = 1;
            $scope.listParams.pageSize = item;
            $scope.paginationConf.itemsPerPage = item;
            $scope.paginationConf.numberOfPages = parseInt($scope.totalItem/$scope.listParams.pageSize);
            getList()
            //  $scope.$broadcast("numberOfPagesChange",{});

        };
        
        $scope.goDetail = function(id){
        	$state.go('eng.postbardeta',{'postbarId':id})
        };
        $scope.goTo = function(state){
            $state.go(state);
        }
    })
});
